package com.hukun.agent;

import java.lang.reflect.Proxy;

public class Test {
    public static void main(String[] args) {

        //1 如何把加载到内存到内存中被代理类，动态的生成一个代理类 Proxy.newProxyInstance
        //2 当通过代理类的对象调用方方法时如何进行动态的调用  InvocationHandler
        CustomerImpl2 customerimpl2 = new CustomerImpl2();
        MyInvocationHandler myInvocationHandler = new MyInvocationHandler(customerimpl2);

        Customer2 customer2 = (Customer2) Proxy.newProxyInstance(customerimpl2.getClass().getClassLoader(), customerimpl2.getClass().getInterfaces(), myInvocationHandler);
        // customer.rend("三室一厅");
        customer2.play("王者");
    }
//动态代理模式
    private static void test2() {
        CustomerImpl customerimpl = new CustomerImpl();
        MyInvocationHandler myInvocationHandler = new MyInvocationHandler(customerimpl);

        Customer customer = (Customer) Proxy.newProxyInstance(customerimpl.getClass().getClassLoader(), customerimpl.getClass().getInterfaces(), myInvocationHandler);
        // customer.rend("三室一厅");
        customer.rend("车");
    }
//静态代理模式
    private static void test1() {
        //代理模式可以在不修改被代理对象的基础上(符合开闭原则)，通过扩展代理类，进行一些功能的附加与增强。

        Customer customer = new Agency(new CustomerImpl());
        customer.rend("三室一厅");
    }
}
